package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ITBOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TBCustomer;
import com.ruoyi.system.service.ITBCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户Controller
 * 
 * @author pspyy
 * @date 2022-02-24
 */
@RestController
@RequestMapping("/system/customer")
@Api("客户管理")
public class TBCustomerController extends BaseController
{
    @Autowired
    private ITBCustomerService tBCustomerService;

    @Autowired
    private ITBOrderService tbOrderService;
    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:list')")
    @GetMapping("/list")
    @ApiOperation("查询客户列表")
    public TableDataInfo list(TBCustomer tBCustomer)
    {
        startPage();
        List<TBCustomer> list = tBCustomerService.selectTBCustomerList(tBCustomer);
        return getDataTable(list);
    }

    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:customer:import')")
    @PostMapping("/importData")
    @ApiOperation("客户导入")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TBCustomer> util = new ExcelUtil<TBCustomer>(TBCustomer.class);
        List<TBCustomer> customerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBCustomerService.importCustomer(customerList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        ExcelUtil<TBCustomer> util = new ExcelUtil<TBCustomer>(TBCustomer.class);
        util.importTemplateExcel(response, "客户数据");
    }
    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("客户数据导入")
    public void export(HttpServletResponse response, TBCustomer tBCustomer)
    {
        List<TBCustomer> list = tBCustomerService.selectTBCustomerList(tBCustomer);
        ExcelUtil<TBCustomer> util = new ExcelUtil<TBCustomer>(TBCustomer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取客户详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBCustomerService.selectTBCustomerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增客户")
    public AjaxResult add(@RequestBody TBCustomer tBCustomer)
    {
        if("0".equals(tBCustomerService.checkCustomerNameUnique(tBCustomer.getCustomerName()))){
            return AjaxResult.error("新增客户'" + tBCustomer.getCustomerName() + "'失败，客户已存在");
        }
        return toAjax(tBCustomerService.insertTBCustomer(tBCustomer));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改客户")
    public AjaxResult edit(@RequestBody TBCustomer tBCustomer)
    {
        return toAjax(tBCustomerService.updateTBCustomer(tBCustomer));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除客户")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = tbOrderService.selectOrderByCustomerIds(ids);
        if(i > 0){
            return AjaxResult.error("客户已存在关联订单，无法删除！");
        }
        return toAjax(tBCustomerService.deleteTBCustomerByIds(ids));
    }
}
